package com.chen.spikemall.controller;

import com.chen.spikemall.bean.RequestOrder;
import com.chen.spikemall.bean.RespModel;
import com.chen.spikemall.exception.NoInventoryException;
import com.chen.spikemall.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OrderController {

    @Autowired
    private GoodsService goodsService;

    @RequestMapping(method = RequestMethod.GET)
    public String index() {
        return "success";
    }


    @RequestMapping(method = RequestMethod.POST, value = "/order")
    public RespModel postOrder(@RequestBody RequestOrder order) {

        RespModel respModel = new RespModel();

        try {
            goodsService.goodsStoreValueDecrease(order.getGoodsNo());
            respModel.setCode("000000");
            respModel.setMsg("success");
        } catch (NoInventoryException e) {
            respModel.setCode("900001");
            respModel.setMsg("此商品没有库存");
        }

        return respModel;
    }

}
